/**

 */
const path = require('path');
const fs = require('fs');
const sqlite3 = require('sqlite3');

// 1. 自动定位 Edge 的 History 文件
const HISTORY = path.join(
  process.env.LOCALAPPDATA,
  'Microsoft/Edge/User Data/Default/History'
);

const copyDB = true;   // 若 Edge 未退出，先复制再读
const tmpDB = HISTORY + '.node';

if (copyDB) fs.copyFileSync(HISTORY, tmpDB);

// 2. 连接并查询
const db = new sqlite3.Database(copyDB ? tmpDB : HISTORY, sqlite3.OPEN_READONLY);
const sql = `
  SELECT url, title, datetime(last_visit_time/1000000-11644473600,'unixepoch','localtime') as local_time
  FROM urls
  ORDER BY last_visit_time DESC
  LIMIT 200;
`;

db.all(sql, (err, rows) => {
  if (err) throw err;
  console.table(rows);   // 漂亮打印
  db.close();
  if (copyDB) fs.unlinkSync(tmpDB); // 清理临时文件
});